package com.SmartCare.service.impl.health;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.common.Result;
import com.SmartCare.common.vo.health.BodyTemperatureVo;
import com.SmartCare.mapper.health.BodyTemperatureMapper;
import com.SmartCare.pojo.health.BodyTemperature;
import com.SmartCare.service.health.BodyTemperatureService;
import com.SmartCare.utils.DateTimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class BodyTemperatureServiceImpl extends ServiceImpl<BodyTemperatureMapper,BodyTemperature> implements BodyTemperatureService {
    @Override
    public Result getTemInfo(Long elderlyId, String startTime, String endTime) {
        if(elderlyId==null) {
            return Result.error("未知错误");
        }

        LambdaQueryWrapper<BodyTemperature> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BodyTemperature::getElderlyId,elderlyId);
        wrapper.between(BodyTemperature::getCreatedTime,startTime,endTime);
        List<BodyTemperature> list = super.list(wrapper);
        if (list.isEmpty()) {
            return Result.error("无记录");
        }

        List<BodyTemperatureVo> bodyTemperatures=new ArrayList<>();

        list.forEach((item)->{
            BodyTemperatureVo vo = new BodyTemperatureVo();
            BeanUtils.copyProperties(item,vo,"createdTime");
            vo.setCreatedTime(DateTimeUtils.formatDateTime(item.getCreatedTime()));
            bodyTemperatures.add(vo);
        });


        return Result.success(bodyTemperatures);
    }

    @Override
    public Result bodyTemperatureAdd(Long elderlyId, Float axillaryTemperature, Float oralTemperature, Float rectalTemperature) {
        if (elderlyId==null) {
            return Result.error("未知错误");
        }
        BodyTemperature item = new BodyTemperature();
        item.setElderlyId(elderlyId);
        if (axillaryTemperature!=null){
            item.setAxillaryTemperature(axillaryTemperature);
        }
        if (oralTemperature!=null) {
            item.setOralTemperature(oralTemperature);
        }
        if(rectalTemperature!=null) {
            item.setRectalTemperature(rectalTemperature);
        }
        boolean res = save(item);
        if (res){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }
}
